<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('合同或协议') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('合同生效时间')" prop="contractEntryTime">
        <DatePicker v-model="paramDate.contractEntryTimes" type="daterange" />
      </el-form-item>
      <el-form-item :label="$t('合同失效时间')" prop="contractTerminationTime">
        <DatePicker v-model="paramDate.contractTerminationTimes" type="daterange" />
      </el-form-item>
      <el-form-item :label="$t('第几次签订')" prop="signNum">
        <el-input
          v-model="queryParams.signNum"
          :placeholder="$t('第几次签订')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('续签时间')" prop="renewalTime">
        <DatePicker v-model="paramDate.renewalTimes" type="daterange" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['hr:contract:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t('新增') }}
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="hrBaseContractList" border>
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column :label="$t('合同生效时间')" align="center" prop="contractEntryTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.contractEntryTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('合同失效时间')" align="center" prop="contractTerminationTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.contractTerminationTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('第几次签订')" align="center" prop="signNum" />
      <el-table-column :label="$t('续签时间')" align="center" prop="renewalTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.renewalTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['hr:contract:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}
          </el-button>
          <el-button
            v-hasPermi="['hr:contract:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改合同或协议对话框 -->
    <el-dialog
      :title="$t(title)"
      :visible.sync="formOpen"
      :close-on-click-modal="false"
      :lock-scroll="false"
      custom-class="dialogForm"
      append-to-body
    >
      <hrBaseContractForm ref="hrBaseContractForm" />
    </el-dialog>
  </div>
</template>

<script>
import { listHrBaseContract, delHrBaseContract, exportHrBaseContract } from '@/api/oa/hr/hrBaseContract'
import hrBaseContractForm from './hrBaseContractForm.vue'

export default {
  components: {
    hrBaseContractForm
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      paramDate: {
        contractEntryTimes: [],
        contractTerminationTimes: [],
        renewalTimes: []
      },
      // 总条数
      total: 0,
      // 合同或协议表格数据
      hrBaseContractList: [],
      // 弹出层标题
      title: '合同或协议',
      // 是否显示弹出层
      formOpen: false,
      // 查询参数
      queryParams: {
        hrId: this.$route.query.hrId,
        contractEntryTime: undefined,
        contractTerminationTime: undefined,
        signNum: undefined,
        renewalTime: undefined,
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询合同或协议列表 */
    getList() {
      this.loading = true
      listHrBaseContract(this.addDateRange(this.queryParams, this.paramDate)).then(response => {
        this.hrBaseContractList = response.rows
        this.total = response.total
        this.loading = false
      })
    },

    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined,
        hrId: this.$route.query.hrId,
        contractEntryTime: undefined,
        contractTerminationTime: undefined,
        signNum: undefined,
        renewalTime: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.paramDate.contractEntryTimes = []
      this.paramDate.contractTerminationTimes = []
      this.paramDate.renewalTimes = []
      this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
      this.formOpen = true
      this.$nextTick(() => {
        this.$refs.hrBaseContractForm.init(null)
      })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
      this.formOpen = true
      this.$nextTick(() => {
        // 调用form页面的 init 方法初始化数据
        this.$refs.hrBaseContractForm.init(row)
      })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
      this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(function() {
        return delHrBaseContract(row.id)
      }).then(() => {
        this.getList()
        this.msgSuccess(this.$t('删除成功'))
      }).catch(function() {
      })
    },
    /** 导出按钮操作 */
    exportHandle() {
      const queryParams = this.queryParams
      this.$confirm(this.$t('是否确认导出数据项?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(function() {
        return exportHrBaseContract(queryParams)
      }).then(response => {
        this.download(response.msg)
      }).catch(function() {
      })
    },
    // 添加日期范围
   addDateRange(params, paramDate) {
      const start = 'Start'
      const end = 'End'
      var search = params
     for (const key in paramDate) {
       const dates = paramDate[key]
       if (dates && dates.length > 0 && dates[0] && dates[1]) {
         search[key + start] = this.dates === undefined ? dates[0] : this.dateRange[0]
         search[key + end] = this.dates === undefined ? dates[1] : this.dateRange[1]
       } else {
         search[key + start] = undefined
         search[key + end] = undefined
       }
     }

      return search
    }
  }
}
</script>
